#include <iostream>
#include "Common.hpp"
#include "InetAddr.hpp"
using namespace std;
void Usage(string proc)
{
    cerr << "Usage:" << proc << "server_ip server_port" << endl;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }

    string serverip = argv[1];
    uint16_t serverport = stoi(argv[2]);

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0)
    {
        cerr << "socket cerr" << endl;
        exit(SOCKET_ERR);
    }
    InetAddr serveraddr(serverip, serverport);
    int n = connect(sockfd, serveraddr.NeAddPtr(), serveraddr.NeAddrLen());
    if (n < 0)
    {
        cerr << "connect error: " << strerror(errno) << endl;
        exit(CONNECT_ERR);
    }
    while (true)
    {
        string line;
        cout << "Please Enter@";
        getline(cin, line);
        write(sockfd, line.c_str(), sizeof(line));
        char buffer[1024];
        int size = read(sockfd, buffer, sizeof(buffer));
        if (size > 0)
        {
            buffer[size] = 0;
            cout << "server echo#" << buffer << endl;
        }
    }
    close(sockfd);
    return 0;
}